# output 1 to 9 timetable
=begin
(1..9).each do |x|
(1..9).each do |y|
print "#{x}X#{y}= "+(x*y).to_s + "  "
print "\n" if y==9
end
end

#output 1 to 9 timetable to a hash
time_table_hash = {}
(1..9).each do |x|
(1..9).each do |y|
time_table_hash[:"#{x}X#{y}"]= x*y
end
end
puts time_table_hash

puts time_table_hash[:"2X8"]


#gets method obtain the standard input
puts "may I have your name please:"
response = gets
puts "Hello, #{response.capitalize}"

#-------------------------------

class BookInStock
	def initialize(isbn, price)
		@isbn = isbn
		@price = price
	end
end

book1 = BookInStock.new("23432recfs",3.45)

p book1
#output: #<BookInStock:0x0a37f0 @isbn="23432recfs", @price=3.45>


#--------------------------------------

class BookInStock
	def initialize(isbn, price)
		@isbn = isbn
		@price = price
	end
	def to_s
		"isbn=#{@isbn}, price=#{@price}"
	end
end

book1 = BookInStock.new("23432recfs",3.45)

puts book1
#output: isbn=23432recfs, price=3.45


#-----------------------------------
class BookInStock
	def initialize(isbn, price)
		@isbn = isbn
		@price = price
	end
	
end

book1 = BookInStock.new("23432recfs",3.45)

# can't do this to access isbn value as it's object's state which can't be accessed from outside()
puts book1.@isbn

# to access isbn and price, we need to define accessor methods

#-----------------------------------------------
# define accessor methods
class BookInStock
	def initialize(isbn, price)
		@isbn = isbn
		@price = price
	end
#this is accessor method
	def isbn
		@isbn
	end
#this is accessor method too
	def price
		@price
	end
end

book1 = BookInStock.new("23432recfs",3.45)

puts "isbn is #{book1.isbn}"
puts "price is #{book1.price}"


#---------------------------------------
# shortcut to create accessor methods - attr_reader

class BookInStock
	attr_reader :isbn, :price
	
	def initialize(isbn, price)
		@isbn = isbn
		@price = price
	end
end

book1 = BookInStock.new("23432recfs",3.45)

puts "isbn is #{book1.isbn}"
puts "price is #{book1.price}"

#---------------------------------
class Person
  #attr_accessor :name
   attr_reader :name
  def initialize(name)
    @name = name
  end

  def say_hello
    puts "Hello, #{name}"
  end
end

peter = Person.new("pete")
peter.say_hello
=end

#-------------------------
require 'csv'
class BookInStock
	attr_reader :isbn, :price
	
	def initialize(isbn, price)
		@isbn = isbn
		@price = price
	end
end

class CsvReader
	
	def initialize
		@book = []
	end
	def read_file(file_name)
		CSV.foreach(file_name, headers: true) do |row|
		@book << BookInStock.new(row["ISBN"], row["Amount"])
		end
		return @book
	end
	def total_value
		sum = 0.0
		@book.each {|a_book|sum += a_book.price.to_f }		
		sum
	end	
end

cr = CsvReader.new
ARGV.each do |file_name|
	STDERR.puts "Processing file #{file_name}..."
	cr.read_file(file_name)
	end
p "Total value is #{cr.total_value}"





